package com.ruoyi.video.server;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.ip.IpUtils;
import com.ruoyi.video.domain.VideoCamera;
import com.ruoyi.video.domain.VideoServer;
import com.ruoyi.video.domain.VideoStream;
import com.ruoyi.video.server.nvr.NvrServer;
import com.ruoyi.video.server.onvif.OnvifServer;
import com.ruoyi.video.server.push.PushServer;
import com.ruoyi.video.server.rtsp.RtspServer;
import com.ruoyi.video.server.utils.StreamUtils;
import com.ruoyi.video.service.IVideoConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import java.util.Map;

/**
 * 服务接口
 */
public abstract class AbstractServer {
    protected VideoServer videoServer;
    protected IVideoConfigService videoConfigService;

    /** 服务端口 **/
    protected Integer wsPort;
    /** 服务IP **/
    protected String wsIP;

    @Value("${server.port}")
    public void setWsPort(Integer wsPort) {
        this.wsPort = wsPort;
    }

    @Autowired
    public void setVideoConfigService(IVideoConfigService videoConfigService){
        this.videoConfigService = videoConfigService;
    }
    public VideoServer getVideoServer(){
        return videoServer;
    }

    /**
     * 服务代码
     * @return
     */
    public abstract String getCode();

    /**
     * 启动
     * @param videoServer
     * @return
     */
    public boolean start(VideoServer videoServer){
        this.videoServer = videoServer;
        return true;
    }


    /**
     * 停止
     * @param videoServer
     * @return
     */
    public abstract boolean stop(VideoServer videoServer);



    /**
     * 判断是否在线
     * @param videoServer
     * @return
     */
    public abstract boolean alive(VideoServer videoServer);



    /**
     * 拉流
     * @param videoCamera 通道号
     * @return
     */
    public VideoStream getStream(VideoCamera videoCamera){
        Map<String,String> config = videoConfigService.selectVideoServerConfig(PushServer.SERVER_CODE);

        if(StringUtils.isNotEmpty(config.get("port"))){
            wsPort = Integer.parseInt(config.get("port"));
        }
        wsIP = config.get("ip");
        if(StringUtils.isEmpty(wsIP)){
            wsIP = IpUtils.getHostIp();
        }

        if(videoCamera.getType().equalsIgnoreCase(RtspServer.SERVER_CODE)
                || videoCamera.getType().equalsIgnoreCase(OnvifServer.SERVER_CODE)
                || videoCamera.getType().equalsIgnoreCase(NvrServer.SERVER_CODE)
        ) {
            VideoStream videoStream = new VideoStream();
            videoStream.setId(videoCamera.getId());
            videoStream.setName(videoCamera.getName());
            videoStream.setMainStream("ws://" + wsIP + ":" + wsPort + "/rtsp/" + videoCamera.getId() + "/real.flv");
            return videoStream;
        }else{
            return null;
        }
    }


    /**
     * 截图
     * @param videoCamera 通道号
     * @return
     */
    public String snap(VideoCamera videoCamera){
        if(videoCamera.getType().equalsIgnoreCase(RtspServer.SERVER_CODE)
                || videoCamera.getType().equalsIgnoreCase(OnvifServer.SERVER_CODE)
                || videoCamera.getType().equalsIgnoreCase(NvrServer.SERVER_CODE)
        ) {
            return StreamUtils.snap(videoCamera.getRtsp());
        }else{
            return null;
        }
    }
}
